<?php

namespace App\Http\Services;

use App\Http\Model\SendPhoneNumber;
use App\Http\Model\SendPhoneUser;
use App\Http\Model\User;

class SendPhoneUserServices extends CommonServices
{
    public function __construct()
    {
        $this->sendPhoneUser = new SendPhoneUser();
        $this->sendPhoneNumber = new SendPhoneNumber();
        $this->user = new User();
    }


    public function verifyUser($user_id, $rank)
    {
        return $this->sendPhoneUser->verifyUser($user_id, $rank);
    }

    public function checkUserPhone($user_id, $phone_id)
    {
        $phone_status = $this->sendPhoneNumber->getPhoneStatus($phone_id);
        if (empty($phone_status)) {
            return 206;
        }

        return $this->sendPhoneUser->checkUser($user_id, $phone_status->phone);
    }

    /*
     * 获取用户资格
     * @param [$user_id]   用户id
     * @param [$config_date]   设置活动时间
     * @param [$timestamp]   充值时间戳
     * @param [$pearl_rank]   充值等级
     * */
    public function getUser($user_id, $config_date = '', $timestamp = '', $pearl_rank)
    {
        if (!$config_date['value']) return false;
        $timestamp = date_ymd($timestamp);
        $date = explode(' - ', $config_date['value']);
        if ($date[1] < $timestamp || $timestamp < $date[0]) {
            //充值时间与获得资格时间不符
            write_log('sendPhoneConsumer', 'timeout', $timestamp);//不在活动时间
            return false;
        }
        $date[1] = $timestamp;
        $user_info = $this->sendPhoneUser->getUser($user_id, $date, $pearl_rank);
        if (!$user_info) {
            write_log('sendPhoneConsumer', 'qualificationError', 'user_id :' . $user_id . 'time:' . $timestamp);//活动资格时间错误
            return false;
        }
        return $user_info;
    }

    /*
     * 修改用户资格
     * @param [$phone]   注册电话
     * @param [$sms_code]   发送验证码
     * @param [$password]   注册密码
     * */
    public function editUser($user_id, $rank = 1)
    {
        $user_info = $this->sendPhoneUser->getUserRank($user_id);
        if($user_info->rank < $rank){
           return $this->sendPhoneUser->edit($user_id, $rank);
        }
        return true;

    }


}